import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
const  Login = ()=>import("@/views/login")

// 首页
const  Home = ()=>import("@/views/home")

// 用户模块
const User = ()=>import("@/views/user/user")

// 角色模块
const Role = ()=>import("@/views/role/role")


// 岗位模块
const Post = ()=>import("@/views/post/post")


// 部门模块
const Dept = ()=>import("@/views/dept/dept")

// 菜单模块
const Menu = ()=>import("@/views/menu/menu")



Vue.use(Router)

const  router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        name: '登录',
      },
      component: Login,
    },
    {
      path: '/',
      redirect: '/login'
    },
    {
        path: '/home',
        name: '首页',
        component: Home,
        iconCls: 'el-icon-tickets',
        children: [{
            path: '/user',
            name: '用户管理',
            component: User,
            meta: {
                requireAuth: true
            }
        },{
          path: '/role',
          name: '角色管理',
          component: Role,
          meta: {
            requireAuth: true
          }
        },{
          path: '/post',
          name: '岗位管理',
          component: Post,
          meta: {
            requireAuth: true
          }
        },{
          path: '/dept',
          name: '部门管理',
          component: Dept,
          meta: {
            requireAuth: true
          }
        },{
          path: '/menu',
          name: '菜单管理',
          component: Menu,
          meta: {
            requireAuth: true
          }
        }]

    }
  ]
})


// 全局前置路由
router.beforeEach((to,from,next)=>{
  console.log("beforeEach:全局前置守卫")
 if(to.path==='/login'){
   next();
 }else{
    // 获取当前用户信息如果没有跳转到登录页面
   if(!store.getters.userId){
      next({path : '/login'});
   }else{
     next();
   }
 }
})


export  default router
